package com.mimosa.util.web;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;

/**
 * 转换HTML中的特殊字符
 * 
 * @author ggeagle
 * 
 */
public final class HTMLCharacterConvertUtil {
	private final Map<String, String> HTML_CHARACTER = new HashMap<String, String>();

	/**
	 * 目前仅转换<和>符号
	 */
	private HTMLCharacterConvertUtil() {
		this.HTML_CHARACTER.put("<", "&#60;");
		this.HTML_CHARACTER.put(">", "&#62;");
	}

	public static final HTMLCharacterConvertUtil INS = new HTMLCharacterConvertUtil();

	/**
	 * 对HTML中的特列字符进行转义
	 * 
	 * @param htmlStr
	 *            包含HTML片段的字符串
	 * @return
	 */
	public String convertHTMLCharacter(String htmlStr) {
		if (StringUtils.isNotBlank(htmlStr)) {
			for (Entry<String, String> entry : this.HTML_CHARACTER.entrySet()) {
				htmlStr = htmlStr.replaceAll(entry.getKey(), entry.getValue());
			}
		} else {
			htmlStr = StringUtils.EMPTY;
		}
		return htmlStr;
	}
}